/*
'  DotNetNuke -  http://www.dotnetnuke.com
'  Copyright (c) 2002-2011
'  by DotNetNuke Corporation
' 
'  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
'  documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
'  the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
'  to permit persons to whom the Software is furnished to do so, subject to the following conditions:
' 
'  The above copyright notice and this permission notice shall be included in all copies or substantial portions 
'  of the Software.
' 
'  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
'  TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
'  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
'  CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
'  DEALINGS IN THE SOFTWARE.
*/

using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Users;
using DotNetNuke.Modules.Store.Providers;
using DotNetNuke.Modules.Store.Providers.Address;

namespace DotNetNuke.Modules.Store.Providers.Address.DefaultAddressProvider
{
	/// <summary>
	/// Summary description for AddresssController.
	/// </summary>
    public sealed class AddressController : ProviderControllerBase, IAddressProvider
	{
		#region Constructors

        public AddressController()
		{
		}

		#endregion

		#region IAddressProvider Members

		public int AddAddress(IAddressInfo addressInfo)
		{
            return DataProvider.Instance().AddAddress(addressInfo.PortalID, addressInfo.UserID, addressInfo.Description, addressInfo.FirstName, addressInfo.LastName, addressInfo.Address1, addressInfo.Address2, addressInfo.City, addressInfo.RegionCode, addressInfo.CountryCode, addressInfo.PostalCode, addressInfo.Email, addressInfo.Phone1, addressInfo.Phone2, addressInfo.PrimaryAddress, addressInfo.UserSaved, addressInfo.CreatedByUser);
		}

		public void UpdateAddress(IAddressInfo addressInfo)
		{
            DataProvider.Instance().UpdateAddress(addressInfo.AddressID, addressInfo.UserID, addressInfo.Description, addressInfo.FirstName, addressInfo.LastName, addressInfo.Address1, addressInfo.Address2, addressInfo.City, addressInfo.RegionCode, addressInfo.CountryCode, addressInfo.PostalCode, addressInfo.Email, addressInfo.Phone1, addressInfo.Phone2, addressInfo.PrimaryAddress, addressInfo.UserSaved);
		}

        public List<T> GetAddresses<T>(int portalID, int userID, string AddressDefaultDescription) where T : IAddressInfo
        {
            // Get user addresses
            List<T> addresses = CBO.FillCollection<T>(DataProvider.Instance().GetAddresses(portalID, userID));

            // Get user registration address
            T addressInfo = (T)GetRegistrationAddress(portalID, userID, AddressDefaultDescription);
            addresses.Insert(0, addressInfo);

            return addresses;
        }

		public IAddressInfo GetAddress(int addressID)
		{
            return CBO.FillObject<AddressInfo>(DataProvider.Instance().GetAddress(addressID));
		}

        public IAddressInfo GetRegistrationAddress(int portalID, int userID, string AddressDefaultDescription)
		{
			UserController controller = new UserController();
			UserInfo userInfo = controller.GetUser(portalID, userID);

            if (userInfo != null)
            {
                AddressInfo addressInfo = new AddressInfo();

                addressInfo.AddressID = 0;
                addressInfo.Description = AddressDefaultDescription;
                addressInfo.FirstName = userInfo.FirstName;
                addressInfo.LastName = userInfo.LastName;
                addressInfo.Address1 = userInfo.Profile.Street;
                addressInfo.Address2 = userInfo.Profile.Unit;
                addressInfo.City = userInfo.Profile.City;
                addressInfo.RegionCode = userInfo.Profile.Region;
                addressInfo.CountryCode = userInfo.Profile.Country;
                addressInfo.PostalCode = userInfo.Profile.PostalCode;
                addressInfo.Email = userInfo.Email;
                addressInfo.Phone1 = userInfo.Profile.Telephone;
                addressInfo.Phone2 = userInfo.Profile.Cell;
                addressInfo.UserID = userInfo.UserID;

                return addressInfo;
            }
            else
            {
                return null;
            }
		}

        public void UpdateRegistrationAddress(int portalID, IAddressInfo addressInfo)
        {
            UserController controller = new UserController();
            UserInfo userInfo = controller.GetUser(portalID, addressInfo.UserID);

            userInfo.FirstName = addressInfo.FirstName;
            userInfo.LastName = addressInfo.LastName;
            userInfo.Profile.Street = addressInfo.Address1;
            userInfo.Profile.Unit = addressInfo.Address2;
            userInfo.Profile.City = addressInfo.City;
            userInfo.Profile.Region = addressInfo.RegionCode;
            userInfo.Profile.Country = addressInfo.CountryCode;
            userInfo.Profile.PostalCode = addressInfo.PostalCode;
            userInfo.Email = addressInfo.Email;
            userInfo.Profile.Telephone = addressInfo.Phone1;
            userInfo.Profile.Cell = addressInfo.Phone2;

            UserController.UpdateUser(portalID, userInfo);
        }

		public void DeleteAddresses(int portalID, int userID)
		{
			DataProvider.Instance().DeleteAddresses(portalID, userID);
		}

		public void DeleteAddress(int addressID)
		{
			DataProvider.Instance().DeleteAddress(addressID);
		}

		public ProviderControlBase GetProfileControl(PortalModuleBase parentControl, string modulePath)
		{
			ProviderControlBase profileControl = loadControl(parentControl, modulePath, "Profile");
			return profileControl;
		}

        public string TokenReplace(string TokenObjectName, string Source, IAddressInfo Address)
        {
            AddressTokenReplace tkAddress = new AddressTokenReplace(TokenObjectName, Address);
            return tkAddress.ReplaceAddressTokens(Source);
        }

		#endregion
	}
}
